﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Web.Services;
using System.Text;
using FlexiCommerce.Components;

namespace FlexiCommerce.Web.DynamicData.FieldTemplates
{
    [ScriptService]
    public partial class Images : FieldTemplateUserControl
    {
        private IList<Components.Image> _images;
        public string ContainerID;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            FlexiCommerce.Resources.Register.JQuery(this.Page);
            FlexiCommerce.Resources.Register.JavaScript(this.Page, "~/FlexiAdmin/Js/jquery.cycle.all.min.js");
        }
        public override void DataBind()
        {
            IList<Components.Image> images = FieldValue as IList<Components.Image>;
            if (images != null)
            {
                _images = images;
                ListViewImages.DataSource = images;
                ListViewImages.DataBind();
            }
            this.ContainerID = ((PersistenceItem)this.Row).ID.ToString();
            base.DataBind();
        }
        protected string GetImageUrl(string id)
        {
            return Page.ResolveUrl("~/flexicommerce/image.ashx?id=" + id + "&size=tiny");
        }
        protected string GetContainerID()
        {
            return this.ClientID + "container";
        }
        public string ImageContainerUniqueID
        {
            get { return this.ClientID; }
        }
        //[WebMethod]
        //[ScriptMethod]
        //public AjaxControlToolkit.Slide[] GetSlides()
        //{
        //    IList<AjaxControlToolkit.Slide> slides = new List<AjaxControlToolkit.Slide>();
        //    foreach (Components.Image i in _images)
        //    {
        //        slides.Add(new AjaxControlToolkit.Slide(Page.ResolveUrl("~/flexicommerce/image.ashx?id=" + i.ID.ToString() + "&size=thumb"),"name","desc"));
        //    }
        //    return slides.ToArray();
        //}
        //protected override void OnPreRender(EventArgs e)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    foreach(Components.Image image in _images)
        //    {
        //        sb.Append("<img src=\"" + GetImageUrl(image.ID.ToString()) + "\" />");
        //    }
        //    LiteralHtml.Text = sb.ToString();
        //    base.OnPreRender(e);
        //}
    }
}